﻿using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading;
using System.Reflection;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Collections.Generic;
using System.Windows.Media.Imaging;

using Cratos.Desktop;

namespace Cratos.Generator
{
	/// <summary>
	/// Splash Window class
	/// </summary>
	public partial class SplashWindow : Window
	{
		#region Class Variable declarations
		private string				m_Version = "";
		private List<string>	m_Tasks		= new List<string>();
		#endregion

		#region Class Constructors & Destructors
		/// <summary>
		/// Class Constructor
		/// </summary>
		public SplashWindow()
		{
			// Initialize the relevant components
			InitializeComponent();
		}
		#endregion

		#region Class Properties
		#endregion

		#region Class Methods
		/// <summary>
		/// This method setup the progress-bar steps.
		/// </summary>
		/// <remarks>
		/// This method is used to clear all the steps currently loaded to be displayed by the splash screen.
		/// </remarks>
		public void ClearSteps()
		{
			// Reset the Steps and the Progress Bar.
			this.m_Tasks.Clear();
			this.m_ProgressBar.Value = 0;
			this.m_ProgressBar.Maximum = 0;
			this.m_ProgressBar.Minimum = 0;

			// Hand over to the OS.
			CratosWPFHelper.DoEvents();

			// Return to calling method.
			return;
		}

		/// <summary>
		/// This method will add a new Splash screen step.
		/// </summary>
		/// <remarks>
		/// This method is used to add a new step to the splash screen step process. These steps
		/// will be displayed by the splash screen and when stepped, the progress bar will be
		/// updated appropriately.
		/// </remarks>
		/// <param name="stepName">The name of the new step</param>
		public void AddStep(string stepName)
		{
			// Add the step to the Step Array.
			this.m_Tasks.Add(stepName);

			// Update the Progress Bar.
			this.m_ProgressBar.Maximum = this.m_Tasks.Count + 1;

			// Hand over to the OS.
			CratosWPFHelper.DoEvents();

			// Return to calling method.
			return;
		}

		/// <summary>
		/// This method will move to the next step.
		/// </summary>
		/// <remarks>
		/// This method is used to move to the next step in the loaded step list.
		/// </remarks>
		public void Step(bool isDelay)
		{
			if (m_ProgressBar.Value < this.m_Tasks.Count)
			{
				// Update the Progress Bar
				this.m_LblInformation.Text	= this.m_Version + "\n" +
																			this.m_Tasks[(int)this.m_ProgressBar.Value].ToString();
				this.m_ProgressBar.Value++;
			}

			// Hand over to the OS.
			CratosWPFHelper.DoEvents();

			// Wait a little ...
			if (isDelay) { Thread.Sleep(200); }

			// Return to calling method.
			return;
		}
		#endregion

		#region Window Event Handlers
		private void OnWindowLoaded(object sender, RoutedEventArgs e)
		{
			// Declare the required variables
			AssemblyName assemblyName;

			// Retrieve the relevant data
			assemblyName = Assembly.GetExecutingAssembly().GetName();

			// Build the version information and set the relevant values
			this.m_Version							= "Version " +
																		assemblyName.Version.Major.ToString() + "." +
																		assemblyName.Version.Minor.ToString().PadLeft(2, '0') + "." +
																		assemblyName.Version.Build.ToString().PadLeft(2, '0');
			this.m_LblInformation.Text	= this.m_Version;

			// Return to calling method
			return;
		}
		#endregion
	}
}
